/*
* Author: Sowmya Kameswaran
*/
package com.foo.CSVWriter;

import java.io.FileWriter;
import java.io.IOException;

import javax.ejb.Stateless;

import com.foo.Entity.TickerInfoData;
import com.foo.Writer.WriterInterface;

/**
 * Session Bean implementation class CSVGenerationModule
 */
@Stateless(name="CSVGenerationModule")
public class CSVGenerationModule implements WriterInterface {//CSVGenerationModuleRemote, CSVGenerationModuleLocal {

    /**
     * Default constructor. 
     */
    public CSVGenerationModule() {
    }
    
        // TODO Auto-generated constructor stub
       @Override
	   public void generateFile(String sFileName ,String[] data, String type)
	   {
		try
		{ 
			// i am using a "type" variable to check what type of operation it is , mainly because the array size is different, if anyone has a better idea u can let me know.
			if(type.equalsIgnoreCase("Spatial"))
			{
		    FileWriter writer = new FileWriter(sFileName);
		    
		    writer.append("Ticker Symbol" + ',');
		    writer.append("Company" +',');
		    writer.append("Sector" +',');
		    writer.append("Industry" +',');
		    writer.append("Country" +',');
		    writer.append("State" +',');
		    writer.append("MarketCap");
	        writer.append('\n');
	        
	        writer.flush();
		    
		    for(int i=0;i<data.length;i+=8)
		    {
		    writer.append(data[i+1] + ',');
		    writer.append(data[i+2] +',');
		    writer.append(data[i+3] +',');
		    writer.append(data[i+4] +',');
		    writer.append(data[i+5] +',');
		    writer.append(data[i+6]+',');
		    writer.append(data[i+7]);
	        writer.append('\n');
		    //generate whatever data you want
	 
		    writer.flush();
		    }
		    writer.close();
			}
			else if(type.equalsIgnoreCase("Temporal")) 
			{
				FileWriter writer = new FileWriter(sFileName);
			    
			    writer.append("Ticker Symbol" + ',');
			    writer.append("Date" + ',');
			    writer.append("Open Value" +',');
			    writer.append("High Value" +',');
			    writer.append("Low Value" +',');
			    writer.append("Close Value" +',');
			    writer.append("Volume");
		        writer.append('\n');
		        
		        writer.flush();
			    
			    for(int i=0;i<data.length;i+=7)
			    {
			    writer.append(data[i] + ',');	
			    writer.append(data[i+1] + ',');
			    writer.append(data[i+2] +',');
			    writer.append(data[i+3] +',');
			    writer.append(data[i+4] +',');
			    writer.append(data[i+5] +',');
			    writer.append(data[i+6]);
		        writer.append('\n');
		
		 
			    //generate whatever data you want
		 
			    writer.flush();
			    }	
			    writer.close();
			}
			else if(type.equalsIgnoreCase("Historical"))
			{
				FileWriter writer = new FileWriter(sFileName);
				
			    writer.append("Date" + ',');
			    writer.append("Ticker Symbol" + ',');
			    writer.append("Open Value" +',');
			    writer.append("High Value" +',');
			    writer.append("Low Value" +',');
			    writer.append("Close Value" +',');
			    writer.append("Volume");
		        writer.append('\n');
		        
		        writer.flush();
			    
			    for(int i=0;i<data.length;i+=7)
			    {
			    writer.append(data[i]);	
			    writer.append(data[i+1] + ',');
			    writer.append(data[i+2] +',');
			    writer.append(data[i+3] +',');
			    writer.append(data[i+4] +',');
			    writer.append(data[i+5] +',');
			    writer.append(data[i+6]);
		        writer.append('\n');
		
		 
			    //generate whatever data you want
		 
			    writer.flush();
			    }
			    writer.close();
			}
			else if(type.equalsIgnoreCase("Snapshot"))
			{
				FileWriter writer = new FileWriter(sFileName);
			    
			    writer.append("Ticker Symbol" + ',');
			    writer.append("Date" + ',');
			    writer.append("Open Value" +',');
			    writer.append("High Value" +',');
			    writer.append("Low Value" +',');
			    writer.append("Close Value" +',');
			    writer.append("Volume");
		        writer.append('\n');
		        
		        writer.flush();
			    
			    for(int i=0;i<data.length;i+=7)
			    {
			    writer.append(data[i] +',');
			    writer.append(data[i+1] + ',');
			    writer.append(data[i+2] +',');
			    writer.append(data[i+3] +',');
			    writer.append(data[i+4] +',');
			    writer.append(data[i+5] +',');
			    writer.append(data[i+6] +',');
		        writer.append('\n');
		
		 
			    //generate whatever data you want
		 
			    writer.flush();
			    }
			    writer.close();
			}
			else if(type.equalsIgnoreCase("Analyzed"))
			{
				FileWriter writer = new FileWriter(sFileName);
			    
			    writer.append("Ticker Symbol" + ',');
			    writer.append("Open Value" +',');
			    writer.append("High Value" +',');
			    writer.append("Low Value" +',');
			    writer.append("Close Value" +',');
			    writer.append("Volume"+',');
			    writer.append("Percentage Change");
		        writer.append('\n');
		        
		        writer.flush();
			    
			    for(int i=0;i<data.length;i+=7)
			    {
			    writer.append(data[i] +',');
			    writer.append(data[i+1] + ',');
			    writer.append(data[i+2] +',');
			    writer.append(data[i+3] +',');
			    writer.append(data[i+4] +',');
			    writer.append(data[i+5] +',');
			    writer.append(data[i+6] +',');
		        writer.append('\n');
		
		 
			    //generate whatever data you want
		 
			    writer.flush();
			    }
			    writer.close();

			}
		}
		catch(IOException e)
		{
		     e.printStackTrace();
		} 
		System.out.println("Storing to CSV was successful");
	    }
       

}
